#include "../pagemempool.h"
#include "../logger.h"

struct Person
{
	std::string name;
	int32_t		age;
	std::string blog;
	char 		buf[1024];
};

PageMemPool<Person>	Person_Allocator;
Person* person_ptrs[6400];

int main( int argc, char * argv[] )
{
	int i;
	init_dailylog( "alloc_test.log", MsgLogger::ELOGLEVEL_DEBUG );
	fprintf( stderr, "g_msglogger = %p\n", g_msglogger );
	usleep( 1000 );
	for( int j = 0; j < 5; ++j )
	{
		for( i = 0; i < 6400; ++i )
		{
			//fprintf( stderr, "g_msglogger = %p\n", g_msglogger );
			person_ptrs[i] = (Person*)Person_Allocator.alloc( __FILE__, __LINE__ );
			if( NULL == person_ptrs[i] )
			{
				fprintf( stderr, "alloc null\n" );
				return 0;
			}
			fprintf( stderr, "alloc person_ptrs[%d] = %p\n", i, person_ptrs[i] );
			LOG_STREAM_DEBUG << i << " ptr=" << std::hex << person_ptrs[i] << LOG_EOM;
		}

		flush_log();

		usleep( 100 );
		for( i = 6400; i > 0; --i )
		{
			//LOG_STREAM_DEBUG << "release " << i << LOG_EOM;
			fprintf( stderr, "release person_ptrs[%d] = %p\n", i-1, person_ptrs[i-1] );
			Person_Allocator.release( (void*)person_ptrs[i-1], __FILE__, __LINE__ );
		}
		usleep( 100 );

		for( i = 0; i < 6400; ++i )
		{
			//LOG_STREAM_DEBUG << "alloc " << i << LOG_EOM;
			person_ptrs[i] = (Person*)Person_Allocator.alloc( __FILE__, __LINE__ );
		}
		usleep( 100 );
	}
	fini_log();
	return 0;
}


